package com.hxalex.simulationhandle.accessbilityService.receiver.search.serivce;

import android.app.IntentService;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.accessibility.AccessibilityNodeInfo;

import com.hxalex.simulationhandle.accessbilityService.command.search.CheckMatchWebSiteIsLoadCommand;
import com.hxalex.simulationhandle.accessbilityService.manger.CommandManger;
import com.hxalex.simulationhandle.accessbilityService.manger.SearchManger;
import com.hxalex.simulationhandle.accessbilityService.receiver.search.CheckMatchWebSiteIsLoadReceiver;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by hx_Alex on 2018/6/1.
 */

public class ClickWebContentService extends IntentService {
    private static toBackPreWebSiteListener toBackPreWebSiteListener;
    private static final int DEAFLAUT_WAIT_TIME=10000;
    private boolean isBlocking=false;
    private int timeCount=0;
    private int count=0;
    public static boolean isStratTiming=false;
    public static boolean isExecuteLoop=true;
    public  static  void setToBackPreWebSiteListener(ClickWebContentService.toBackPreWebSiteListener toBackPreWebSiteListener) {
        ClickWebContentService.toBackPreWebSiteListener = toBackPreWebSiteListener;
    }
    
    public ClickWebContentService() {
        super("ClickWebContentService");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent)
    {
        while (isExecuteLoop) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
 
            SearchManger searchManger = SearchManger.getSearchManger(this);
            int waitTime = searchManger.getWebSiteStopTime() * 1000;
            if(timeCount>waitTime && isBlocking)
            {
                Log.e("ClickWebContentService", "计时："+timeCount+"回退....");
//                toBackPreWebSiteListener.toBackPreWebSite();
//                toBackPreWebSiteListener.toBackPreWebSite();
              
                timeCount=0;
                isStratTiming=false;
                isBlocking=false;
                EventBus.getDefault().post(SearchManger.OPEN_WAKE_WINWODS);
                //重新加载搜索命令，退出服务
                CommandManger manger = CommandManger.getManger();
                searchManger.getContentMatchList().clear();
                manger.loadBrowserCommandAgain(this,true);
                manger.loadBrowserCommand(this,true);
                break;
            }else if(isBlocking && isStratTiming)
            {
                timeCount+=1000;
                continue;
            }else  if(isBlocking && !isStratTiming)
            {
                continue;
            }
            
            //获取匹配到的网站库
            AccessibilityNodeInfo info = searchManger.nowClickString;
            boolean reslut = searchManger.performActionClick(searchManger.nowClickString);
            String text="";
            if (info.getText() != null) {
                text=info.getText().toString();
            }else if(info.getContentDescription()!=null)
            {
                text=info.getContentDescription().toString();
            }
            
            Log.e("performActionClick", "reslut :"+reslut+" 尝试点击的项为:"+text+ " isClick:"+info.isClickable());
                if (reslut) {
                    Log.e("performActionClick", text);
                    CommandManger manger = CommandManger.getManger();
                    manger.getCommandQueue().offer(new CheckMatchWebSiteIsLoadCommand(searchManger,new CheckMatchWebSiteIsLoadReceiver(searchManger)));
                    isBlocking=true;
                }else
                {
                    Log.e("ClickWebContentService", "点击错误");
                    searchManger.openBrowser();
                    CommandManger manger = CommandManger.getManger();
                    manger.getCommandQueue().clear();
                    searchManger.getContentMatchList().clear();
                    manger.loadBrowserCommand(this,true);
                    break;
                }
                searchManger.nowClickString=null;
        }
    }


    public interface toBackPreWebSiteListener
    {
        void toBackPreWebSite();
    }
    
}
